package com.green.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.green.system.common.Constant;
import com.green.system.common.DataGridView;
import com.green.system.domain.Role;
import com.green.system.mapper.RoleMapper;
import com.green.system.service.RoleService;
import com.green.system.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * @Author: Green
 * @Date: 2021/10/30/15:37
 * @Description: 
 */


@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService{

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public DataGridView queryAllRole(RoleVo roleVo) {
        IPage<Role> page = new Page<>(roleVo.getPage(),roleVo.getLimit());
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(roleVo.getAvailable()!=null,"available",roleVo.getAvailable());
        queryWrapper.like(StringUtils.isNotBlank(roleVo.getName()),"name",roleVo.getName());
        queryWrapper.like(StringUtils.isNotBlank(roleVo.getRemark()),"remark",roleVo.getRemark());
        this.roleMapper.selectPage(page,queryWrapper);
        return new DataGridView(page.getTotal(),page.getRecords());
    }

    @Override
    public Role saveRole(Role role) {
        this.roleMapper.insert(role);
        return role;
    }

    @Override
    public Role updateRole(Role role) {
        this.roleMapper.updateById(role);
        return role;
    }

    @Override
    public List<Integer> queryMenuIdsByRid(Integer id) {
        return this.roleMapper.queryMenuIdsByRid(id);
    }

    /**
     * 保存角色和菜单之间的关系
     * @param rid
     * @param mids
     */
    @Override
    public void saveRoleMenu(Integer rid, Integer[] mids) {
        //先删除再添加，防止主键冲突
        this.roleMapper.deleteRoleMenuByRid(rid);
        if (null != mids && mids.length>0){
            for (Integer mid : mids) {
                this.roleMapper.insertRoleMenu(rid,mid);
            }
        }
    }

    @Override
    public DataGridView queryAllAvailableRoleNoPage(RoleVo roleVo) {
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(roleVo.getAvailable()!=null,"available",roleVo.getAvailable());
        List<Role> roles = this.roleMapper.selectList(queryWrapper);
        //根据用户ID查询已拥有的角色ID
        List<Integer> roleIds = this.roleMapper.queryRoleIdsByUserId(roleVo.getUserId());

        List<Map<String,Object>> lists = new ArrayList<>();
        for (Role role : roles) {
            Boolean LAY_CHECKED =false;
            for (Integer roleId : roleIds) {
                if (role.getId().equals(roleId)){
                    LAY_CHECKED = true;
                    break;
                }
            }
            //当前角色标志设置后,重新返回JSON字符串
            Map<String,Object> map = new HashMap<>();
            map.put("id",role.getId());
            map.put("name",role.getName());
            map.put("remark",role.getRemark());
            map.put("LAY_CHECKED",LAY_CHECKED);
            lists.add(map);
        }
        return new DataGridView(Long.valueOf(roles.size()),lists);

    }

    @Override
    public List<String> queryRoleNamesByUserId(Integer id) {
        //根据用户ID查询角色ID的集合
        List<Integer> roleIds = this.roleMapper.queryRoleIdsByUserId(id);
        if (null != roleIds && roleIds.size()>0){
            QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("available", Constant.AVAILABLE_TRUE);
            queryWrapper.in("id",roleIds);
            List<Role> rolesObj = this.roleMapper.selectList(queryWrapper);
            List<String> roles = new ArrayList<>();
            for (Role role : rolesObj) {
                roles.add(role.getName());
            }
            return roles;
        }else {
            return null;
        }
    }


    @Override
    public boolean removeById(Serializable id) {

        //根据角色ID删除角色和菜单之间的关系
        roleMapper.deleteRoleMenuByRid(id);
//        roleMapper.deleteRoleMenuByMid(id);

        //根据角色ID删除角色和用户之间的关系
        roleMapper.deleteRoleUserByRid(id);
//        roleMapper.deleteRoleUserByUid(id);

        return super.removeById(id);
    }
}
